package com.zego.wawaji.third.zego.im.callback;

import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.third.zego.im.listener.ZegoListener;
import com.zego.zegoliveroom.callback.im.IZegoIMCallback;
import com.zego.zegoliveroom.constants.ZegoIM;
import com.zego.zegoliveroom.entity.ZegoBigRoomMessage;
import com.zego.zegoliveroom.entity.ZegoConversationMessage;
import com.zego.zegoliveroom.entity.ZegoRoomMessage;
import com.zego.zegoliveroom.entity.ZegoUserState;

/**
 * 通信回调
 */
public class ZegoIMCallack implements IZegoIMCallback {
    private int audienceNum = 0;   // 初始用户数
    private ZegoListener listener;

    public ZegoIMCallack(ZegoListener listener) {
        this.listener = listener;
    }

    public void setAudienceNum(int audienceNum) {
        this.audienceNum = audienceNum;
    }

    public void release() {
        listener = null;
    }

    /**
     * 房间成员更新回调
     */
    @Override
    public void onUserUpdate(final ZegoUserState[] zegoUserStates, int updateType) {
        MsgMgr.getInstance().runOnUiThread(() -> {
            String temp = "";
            for (ZegoUserState state : zegoUserStates) {
                if (state.updateFlag == ZegoIM.UserUpdateFlag.Added) {
                    temp = "加入房间";
                    audienceNum++;
                    MsgMgr.getInstance().sendMsg(MsgType.MT_User_JoinIn, state.userName + "：" + temp);
                } else if (state.updateFlag == ZegoIM.UserUpdateFlag.Deleted) {
                    temp = "离开房间";
                    audienceNum--;
                }
            }
            if (listener != null)
                listener.roomTotalNum(audienceNum < 0 ? 0 : audienceNum);
        });
    }

    /**
     * 收到房间的广播消息
     */
    @Override
    public void onRecvRoomMessage(String roomId, ZegoRoomMessage[] zegoRoomMessages) {
        for (ZegoRoomMessage roomMessage : zegoRoomMessages) {
            MsgMgr.getInstance().sendMsg(MsgType.MT_Msg_Receive, roomMessage.content);
        }
    }

    /**
     * 收到会话消息
     */
    @Override
    public void onRecvConversationMessage(String s, String s1, ZegoConversationMessage zegoConversationMessage) {
    }

    @Override
    public void onUpdateOnlineCount(String s, int i) {
    }

    @Override
    public void onRecvBigRoomMessage(String s, ZegoBigRoomMessage[] zegoBigRoomMessages) {
    }
}
